/**
 * @Description:
 * @Date:Create in 16:10  2022/7/7
 */
public class replaceSpace_offer_05 {

    public static void main(String[] args) {
        String s = "We are happy.";
        String s1 = replaceSpace1(s);
        System.out.println(s1);
    }


    public static String replaceSpace1(String s) {
        char[] ch1 = s.toCharArray();
        int size = ch1.length;

        for (char a : ch1) {
            if (a == ' ') {
                size += 2;
            }
        }

        char[] ch2 = new char[size];
        for (int i = 0, j = 0; i < size; i++) {
            if (ch1[j] == ' ') {
                ch2[i] = '%';
                ch2[i + 1] = '2';
                ch2[i + 2] = '0';
                i += 2;
            } else {
                ch2[i] = ch1[j];
            }
            j++;
        }

        return new String(ch2);
    }

}
